const http = require('http')
const db = require('../封装bd模块/db')
const dayjs = require('dayjs')
//创建web服务器实例
const server = http.createServer()

//启动
server.listen(6635,()=>{
console.log('服务器已启动,http://10.254.2.67:6635');
})
//给服务器绑定事件
server.on('request',(request,response)=>{
    let {url,method} = request
    // console.log(request.url);
    let data = {}
    if(method.toLowerCase() === 'get'){
        if(url === '/api/news'){
           data = db.news()
           data.forEach(el => {
            el.publish_time = dayjs(el.publish_time).format('YYYY-MM-DD HH:mm:ss')
           })
        }else if(url === '/api/students'){
            data = db.students()
        }else if(url === '/api/lucystar'){
            data = db.lucystar()
        }
    }
response.setHeader('Access-Control-Allow-Origin','*')
response.setHeader('content-type','text/plain;charset=utf-8')
response.end(JSON.stringify(data))
})
